import { Component, OnInit } from '@angular/core';
import {DrawService, TipService,ModalTipService,DcTablePagingPipe,ModalService,HttpService} from 'itcloud-plugin';
import { GetPathService } from '../get-path.service';
import { ReplyNoticeComponent } from '../my-todo/reply-notice/reply-notice.component';

@Component({
  selector: 'app-home-notice',
  templateUrl: './home-notice.component.html',
  styleUrls: ['./home-notice.component.css']
})
export class HomeNoticeComponent implements OnInit {
  //分页
  pager;
  tableId: string;
  tableLoading: boolean;
  cookies:any;
  tableOptions = {
      // maxHeight: '200px',
      // minHeight: '100px',
      checkbox: false,
      loading: true,
      showIndex: false
  };
  param:any={
      searchType:1,
      limit: 10,
      page:1
  }

  constructor(
    private drawService: DrawService,
    private modal: ModalService,
    private http:HttpService,
    private GetPathService:GetPathService,
    private tipService:TipService
  ) { }
  headerConfig = [
    {
        field: 'infoName', // 显示字段
        title: '公告主题', // 表头显示
        width: '70%', // 单元格宽度
        hideTitle: false,
        flex: '1' // 单元格自适应宽度优先级,
    },
    {
      field: 'infoType', // 显示字段
      title: '类型', // 表头显示
      canSort: false, // 是否可排序
      hideTitle: true,
      width: '15%' // 单元格宽度
  },  
    {
        field: 'createName', // 显示字段
        title: '创建人', // 表头显示
        width: '15%' // 单元格宽度
    }
  ];
  tableData = [];

  ngOnInit() {
    this.cookies= this.getCookie('userId');
    this.param.userId= this.cookies;
    this.pager = {
      total: 0,
      limit: 10,
      page: 1
    };
    this.getDataInit();
  }
  getCookie(name){
      let arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
      if(arr=document.cookie.match(reg))return decodeURI(arr[2]);
      else return null;
  }
  getDataInit(){
    this.http.getData({
      url: this.GetPathService.AjaxPath.bulletinServer+'/mineTodo/mineTodoList',
      search: this.param,
    }).subscribe(res => {
      this.tableOptions.loading = false;
      if(res['returnCode'] === '0000'){
        this.pager.total= res['result'].allRows;
        this.disposeData(res['result'].result);
      }else{
        this.tipService.show({ type: 'error', title: res['returnMessage']}); 
      }
      //this.annex = res['result'] && res['result'].demandAttach;
    }, err => {this.tipService.show({ type: 'error', title: "加载失败"});})
  }
  disposeData(data){
    for(let i=0;i<data.length;i++){
      data[i].infoTypeName = (data[i].infoType === 0 ? '公告通知' : '信息交流');
    }
    this.tableData = data;
  }
  //获取节点
  findItemByKey(dataList) {
    let title = dataList.infoType ===1 ? ' 回复公告': ' 查看公告'
    const result = this.drawService.open({
      handler: (type) => { // 传入侧滑component的方法，在component中用@Input() handler:any;接收
        result.closeComponent();
        this.getDataInit();
      },
      data: dataList,
      component: ReplyNoticeComponent, // 侧滑内容的component
      iconCls: '',
      title: title, // 侧滑是否有title
      padding: '10px 0 0 0', // 侧滑内容的padding值
      width: '80%', // 侧滑宽度
      hiddenScroll: false, // 隐藏侧滑的滚动条，使用程序自己的滚动条
    });
  }
  // 切换页码
  nowPageChange(ev) {
    this.pager.page = ev;
    this.param.page = ev;
    this.getDataInit();
    // this.getAdviceTableData();
}

}
